<template>
  <div id="adminUserPage">
    <a-form :model="page" layout="inline">
      <a-form-item field="appId" label="应用Id" style="min-width: 300px">
        <a-input v-model="page.appId" placeholder="请输入应用Id" />
      </a-form-item>
      <a-form-item field="userId" label="用户Id" style="min-width: 300px">
        <a-input v-model="page.userId" placeholder="请输入用户Id" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSearch">搜索</a-button>
      </a-form-item>
    </a-form>

    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <template #questionContent="{ record }">
        <a-tooltip :content="record.questionContent">
          <a-button>题目</a-button>
        </a-tooltip>
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button status="danger" @click="doDelete(record)">删除</a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  deleteUserUsingPost,
  listUserByPageUsingPost,
} from "@/api/userController";
import message from "@arco-design/web-vue/es/message";
import {
  deleteQuestionUsingPost,
  listQuestionByPageUsingPost,
} from "@/api/questionController";

const searchParams = ref({
  pageSize: 10,
  current: 1,
});

const page = ref({
  appId: "",
  userId: "",
});

const total = ref();

const dataList = ref();

const loadData = async () => {
  const res = await listQuestionByPageUsingPost(searchParams.value);
  if (res.data.code == 0) {
    dataList.value = res.data?.data?.records;
    total.value = res.data?.data?.total;
  } else {
    message.error("获取数据失败" + res.data.message);
  }
};

onMounted(() => {
  loadData();
});

/**
 * 监听 searchParams 变量，改变时触发数据的重新加载
 */
watchEffect(() => {
  loadData();
});

/**
 * 当分页变化时，改变搜索条件，触发数据加载
 */
const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const columns = [
  {
    title: "id",
    dataIndex: "id",
  },
  {
    title: "题目内容",
    dataIndex: "questionContent",
    slotName: "questionContent",
    // 可能需要使用一个富文本或JSON解析库来展示题目内容
  },
  {
    title: "应用id",
    dataIndex: "appId",
  },
  {
    title: "创建用户id",
    dataIndex: "userId",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    // 可以添加日期格式化函数
  },
  {
    title: "更新时间",
    dataIndex: "updateTime",
    // 可以添加日期格式化函数
  },
  {
    title: "操作",
    slotName: "optional",
  },
];

//搜索
const doSearch = () => {
  searchParams.value = {
    ...searchParams.value,
    ...page.value,
    current: 1, //将当前页设置为1
  };
};

//删除
const doDelete = async (record: any) => {
  const res = await deleteQuestionUsingPost({
    id: record.id,
  });
  if (res.data.code == 0) {
    loadData();
    message.success("删除成功");
  } else {
    message.error("删除失败" + res.data.message);
  }
};
</script>
<style scoped></style>
